import { getBrandList } from '@/services/brand';
import { getTagList } from '@/services/tag';
import { getUserList } from '@/services/user';
import { ResponseCode } from '@/utils/request';
import { useCallback } from 'react';

// 检索品牌
export function useBrandFetchOptions() {
  return useCallback(async (searchStr: string) => {
    let property = 'name';
    if (!isNaN(Number(searchStr))) {
      property = 'id';
    }
    const { data, code } = await getBrandList({
      [property]: searchStr,
      pageSize: 10,
      current: 1,
    });
    if (code === ResponseCode.Fail) return [];
    return data.list.map((item) => ({
      ...item,
      value: item.id,
      label: `${item.name}(剩余库存: ${item.stock})`,
    }));
  }, []);
}

// 检索标签
export function useTagFetchOptions() {
  return useCallback(async (searchStr: string) => {
    let property = 'name';
    if (!isNaN(Number(searchStr))) {
      property = 'id';
    }
    const { data, code } = await getTagList({
      [property]: searchStr,
      pageSize: 10,
      current: 1,
    });
    if (code === ResponseCode.Fail) return [];
    return data.list.map((item) => ({
      ...item,
      value: item.id,
      label: `${item.name}`,
    }));
  }, []);
}

// 检索用户
export function useUserFetchOptions() {
  return useCallback(async (searchStr: string) => {
    let property = 'name';
    if (!isNaN(Number(searchStr))) {
      property = 'id';
    }
    const { data, code } = await getUserList({
      [property]: searchStr,
      pageSize: 10,
      current: 1,
    });
    if (code === ResponseCode.Fail) return [];
    return data.list.map((item) => ({
      ...item,
      value: item.id,
      label: `${item.nickname}`,
    }));
  }, []);
}
